/* 
 * File:   DRTSection.h
 * Author: Jeffrey Geyssens
 *
 * Created on May 13, 2010, 9:07 PM
 *
 * This is a container for DRT's used in MAP file.
 * The container is a map. The key is the string corresponding to the DRTs id.
 * The data stored in map is actual DRT element.
 */

#ifndef _DRTSECTION_H
#define	_DRTSECTION_H

#include <map>   
#include <string>
#include "DataReferenceTerminal.h"


using namespace std;

class DRTSection {
public:
    DRTSection();
    DRTSection(const DRTSection& orig);
    virtual ~DRTSection();
    void print();
    void buildFile(string filename);
    void buildSpec(string filename);
    void addSymDef(SymbolDefinition& SD);
    void newDRT(string s);
    void printDRTlist();
    void printDRTlist(int entryNo);
    string getDRTid(int entryNo);
    void setTable(string DRT, string table);
    void setSymAttrib(string DRT, string sym);
    void setDefAttrib(string DRT, string def);
    void setPK(string DRT, string PK);
    vector<string> getDML();




private:
    map<string, DataReferenceTerminal> section;
    bool exists(string DRTid);
    void addDRT(string DRTid);

};

DRTSection::DRTSection() {

}

DRTSection::DRTSection(const DRTSection& orig) {
    section = orig.section;
}

DRTSection::~DRTSection() {

}

void DRTSection::printDRTlist() {
    int i;
    i = 1;
    map<string, DataReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        cout << i << ") " << (*it).first << endl;
        i++;
    }
}

void DRTSection::printDRTlist(int entryNo) {
    int i;
    i = 1;
    map<string, DataReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        if (i == entryNo) {
            DataReferenceTerminal& DRT = (*it).second;
            cout << DRT << endl;
            cout << "table: " << (*it).second.getTable();
            cout << endl;
            cout << "symbol-attribute: " << (*it).second.getSymAttribute();
            cout << endl;
            cout << "definition-attribute: " << (*it).second.getDefAttribute();
            cout << endl;
            (*it).second.printSymDef();
        }
        i++;
    }
}

string DRTSection::getDRTid(int entryNo) {
    int i;
    i = 1;
    map<string, DataReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        if (i == entryNo) {
            DataReferenceTerminal& DRT = (*it).second;
            return DRT.getId();
        }
        i++;
    }
}

void DRTSection::print() {
    cout << ".DRTMATCH" << "\n\n";
    map<string, DataReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        DataReferenceTerminal& d = (*it).second;
        cout << d << endl;


    }
    cout << ".END_DRTMATCH" << "\n";
}

void DRTSection::buildFile(string filename) {
    //this->print();
    ofstream myfile(filename.c_str(), ios::app);
    myfile << ".DRTMATCH" << "\n\n";
    myfile.close();
    multimap<string, DataReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        DataReferenceTerminal& d = (*it).second;
        d.buildFile(filename);
    }
    myfile.open(filename.c_str(), ios::app);
    myfile << ".END_DRTMATCH" << "\n\n";
    myfile.close();
}

void DRTSection::buildSpec(string filename) {
    this->print();
    ofstream myfile(filename.c_str(), ios::app);
    multimap<string, DataReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end();
            ++it) {
        DataReferenceTerminal& d = (*it).second;
        cout << "Build: " << d << endl;
        d.buildSpec(filename);
        myfile.close();
    }
}

bool DRTSection::exists(string DRTid) {
    map<string, DataReferenceTerminal>::iterator it;
    it = section.find(DRTid);
    if (it != section.end())
        return true;
    else
        return false;
}

void DRTSection::addDRT(string DRTid) {
    DataReferenceTerminal DRT;
    DRT.setId(DRTid);
    section.insert(pair<string, DataReferenceTerminal > (DRTid, DRT));
}

void DRTSection::addSymDef(SymbolDefinition& SD) {
    map<string, DataReferenceTerminal>::iterator it;
    //DRT add stuff
    if (false == this->exists(SD.getDRT())) {
        this->addDRT(SD.getDRT());
    }

    it = section.find(SD.getDRT());
    DataReferenceTerminal& DRT = it->second;
    DRT.addSymDef(SD);

    //hier fout want terug steken in section
    //section.erase(it);
    //ection.insert(pair<string, DataReferenceTerminal > (DRT.getId(), DRT));
}

void DRTSection::newDRT(string s) {

    this->addDRT(s);

}

void DRTSection::setTable(string DRT, string table) {
    map<string, DataReferenceTerminal>::iterator it;
    if (this->exists(DRT)){
    it = section.find(DRT);
    (*it).second.setTable(table);
    }
}

void DRTSection::setPK(string DRT, string PK) {
    map<string, DataReferenceTerminal>::iterator it;
    if (this->exists(DRT)){
    it = section.find(DRT);
    (*it).second.setPK(PK);
    }
}

void DRTSection::setSymAttrib(string DRT, string symbol) {
    if (this->exists(DRT)){
    map<string, DataReferenceTerminal>::iterator it;
    it = section.find(DRT);
    (*it).second.setSymAttrib(symbol);
    }
}

void DRTSection::setDefAttrib(string DRT, string def) {
    if (this->exists(DRT)){
    map<string, DataReferenceTerminal>::iterator it;
    it = section.find(DRT);
    (*it).second.setDefAttrib(def);
    }
}

vector<string> DRTSection::getDML() {
    cout << "Scanning DRT Section..." << endl;
    vector<string> SQL;
    map<string, DataReferenceTerminal>::iterator it;
    for (it = section.begin(); it != section.end(); ++it) {
        DataReferenceTerminal& d = (*it).second;
        vector<string> DRTDML = d.getDML();
        vector<string>::iterator i;
        for (i = DRTDML.begin(); i != DRTDML.end(); ++i){
            string sql = (*i);
            SQL.push_back(sql);
        }

    }
    return SQL;


}



#endif	/* _DRTSECTION_H */

